package com.lupan.mutithred.cp8_ScheduledExecutorService;

import java.util.concurrent.*;

/**
 * 说明：
 *
 * @author lupan
 * @version 2017/5/11 10:32
 */
public class Main {
    public static class MyRunnable implements Runnable {

        private String name;

        public MyRunnable(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println("线程" + name + " 开始，时间" + System.currentTimeMillis());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程" + name + " 结束，时间" + System.currentTimeMillis());
        }
    }

    public static void main(String[] args) throws InterruptedException {

        MyRunnable myRunnableA = new MyRunnable("A");
        MyRunnable myRunnableB = new MyRunnable("B");

        ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(5);

        pool.scheduleAtFixedRate(myRunnableA,2,1, TimeUnit.SECONDS);
        ScheduledFuture future = pool.scheduleAtFixedRate(myRunnableB, 5, 1, TimeUnit.SECONDS);

//        pool.scheduleWithFixedDelay(myRunnableA,2,1,TimeUnit.SECONDS);
//        pool.scheduleWithFixedDelay(myRunnableB,2,1,TimeUnit.SECONDS);

        Thread.sleep(12000);

        //注意：remove的方法参数是ScheduledFuture类型
        System.out.println(pool.remove((Runnable)future));
        BlockingQueue<Runnable> queue = pool.getQueue();


    }
}
